package cc.idoone.spring_boot_hello.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @auther hemj
 * @since 2017/9/26
 */
@Service
public class RedisServiceSimpleImpl implements RedisService {

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Override
	public boolean set(final String key, final String value) {
		stringRedisTemplate.opsForValue().set(key, value);
		stringRedisTemplate.opsForValue().set(key, value, 10, TimeUnit.SECONDS);
		return true;
	}

	public String get(final String key) {
		return stringRedisTemplate.opsForValue().get(key);
	}

	@Override
	public boolean expire(final String key, long expire) {
		return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
	}

	@Override
	public <T> boolean setList(String key, List<T> list) {
		ValueOperations<String, List> operations = redisTemplate.opsForValue();
		operations.set(key, list);
		return true;
	}

	@Override
	public <T> List<T> getList(String key, Class<T> clz) {
		ValueOperations<String, List> operations = redisTemplate.opsForValue();
		return operations.get(key);
	}

	@Override
	public long lpush(final String key, Object obj) {
		return redisTemplate.opsForList().leftPush(key, obj);
	}

	@Override
	public long rpush(final String key, Object obj) {
		return redisTemplate.opsForList().rightPush(key, obj);
	}

	@Override
	public String lpop(final String key) {
		return String.valueOf(redisTemplate.opsForList().leftPop(key));
	}

}
